<?php defined('SYSPATH') or die('No direct script access.');

class Model_Task extends Model_Entity
{
	public function get_task($task_id)
	{
		$task['general_info'] = DB::select()->from('entity_task')->where('id','=', $task_id)->execute()->current();
		$task['details'] = DB::select()->from('task_project')
			->where('task_id','=', $task_id)->execute()->as_array();
		return $task;
	}
	public function suspend_task_user($user_id, $task_id)
	{
		return DB::update('task_project')->set(array('is_suspended' => Helper_MyConst::TASK_SUSPENDED))
			->where('task_id','=',$task_id)->and_where('user_id','=',$user_id)->execute();
	}
	public function add_task($info)
	{
		$user_info = Model::factory('user')->get_user_info($info['assign_user_id']);
		$info['assign_user_displayname'] 	= $user_info['displayname'];
		$info['assign_user_profile_image']	= $user_info['profile_image'];
		$user_info = Model::factory('user')->get_user_info($info['user_id']);
		$info['user_displayname'] 	= $user_info['displayname'];
		$info['user_profile_image']	= $user_info['profile_image'];
		$info['status']				= Helper_MyConst::TASK_ON_PROGRESS;
		return $ret = $this->add_entity(Helper_MyConst::ENTITY_TYPE_TASK, $info);
	}
	public function get_project_tasks($project_id)
	{
		$query = DB::query(Database::SELECT, '
			SELECT entity_task.*, group.name as group_name, group.order_id as order_id 
			FROM `entity_task` JOIN `group` ON entity_task.group_id = group.id
			WHERE entity_task.project_id = :project_id ORDER BY order_id DESC, status DESC, date_create DESC');
		$query->parameters(array(':project_id' => $project_id));
		$tasks = $query->execute()->as_array();
		return $tasks;
	}
	public function get_project_group($project_id)
	{
		$group = DB::select()->from('group')->where('project_id','=', $project_id)
				->execute()->as_array();
		return $group;
	}
	public function add_group($group_name, $project_id, $user_id)
	{
		$last_group = DB::select()->from('group')->order_by('id', 'DESC')->limit(1)->execute()->as_array();
		$order = (count($last_group) == 0) ? 1 : $last_group[0]['order_id'] + 1;
		
		$info['name'] 		= $group_name;
		$info['project_id']	= $project_id;
		$info['user_id']	= $user_id;
		$info['order_id']	= $order;
		
		return $this->add('group', $info);
	}
	public function assign_task($task_id, $user_id)
	{
		$user_info = Model::factory('user')->get_user_info($user_id);
		$info = array(
			'task_id'						=> $task_id,
			'assign_user_id'				=> $user_id, 
			'assign_user_displayname' 		=> $user_info['displayname'],
			'assign_user_profile_image'		=> $user_info['profile_image'],
			'date_assign'					=> DB::expr('NOW()'),
			'assign_task_status'			=> Helper_MyConst::TASK_ON_PROGRESS
		);
		return $this->add('task_project_user', $info);
	}
	public function edit_task($info)
	{
		$task = DB::select()->from('task')->where('id','=',$info['id'])->execute()->current();
		if ($task['assign_user_id'] != $info['assign_user_id'])
		{
			$user = Model::factory('user')->get_user_info($info['assign_user_id']);
			$info['assign_user_displayname'] 	= $user['displayname'];
			$info['assign_user_profile_image'] 	= $user['profile_image'];
		}
		return $this->edit('task', $info['id'], $info);
	}
	public function delete_task($task_id)
	{
		$ret = DB::delete('task_project')->where('id', '=', $task_id)->execute();
		if($ret)
			$ret = DB::delete('task')->where('id','=',$task_id);
		return $ret;
	}
	public function get_user_tasks($user_id, $project_id = NULL)
	{
		$query = DB::select()->from('entity_task')->join('task_project')->on('entity_task.id','=','task_project.task_id')
			->where('entity_task.user_id','=',$user_id);
		if (isset($project_id))
			$query->and_where('task_project.project_id','=',$project_id);
		$query->order_by('entity_task.date_create');
		return $ret = $query->execute()->as_array();
	}
}